package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsInspectTaskDetailMapper;
import cc.rengu.igas.bsps.common.entity.InspectTaskDetail;
import cc.rengu.igas.bsps.common.enums.InspectStatusEnum;
import cc.rengu.igas.bsps.common.enums.InspectTypeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangxuran on 2020/04/04
 */
public class BspsInspectTaskDetailMapperImpl implements BspsInspectTaskDetailMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public InspectTaskDetail selectBspsInspectTaskDetailByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsInspectTaskDetailByMchntNo");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setMchntNo(mchntNo);
        inspectTaskDetail.setInspectType(InspectTypeEnum.ONLINE_INSPECT.getInspectType());
        inspectTaskDetail.setInspectStatus(InspectStatusEnum.UNSUBMIT_STATUS.getStatusCode());
        inspectTaskDetail.setRemark5(InspectStatusEnum.REFUSED_STATUS.getStatusCode());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, inspectTaskDetail);
        if (0 != iReturnCode) {
            rglog.error("获取巡检任务详情信息失败,mchntNo:<{}>,iReturnCode:<{}>", mchntNo, iReturnCode);
            return null;
        }
        return inspectTaskDetail;
    }

    @Override
    public int updateBspsInspectTaskDetailByMchntNoAndTaskNo(InspectTaskDetail inspectTaskDetail) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsInspectTaskDetailByMchntNoAndTaskNo1");

        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, inspectTaskDetail, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户巡检任务详情信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectTaskDetail.getInstId(), inspectTaskDetail.getInspectTaskNo(), inspectTaskDetail.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    public int CheckBspsInspectTaskDetailByMchntNoAndTaskNo(InspectTaskDetail inspectTaskDetail) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "CheckBspsInspectTaskDetailByMchntNoAndTaskNo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectTaskDetail, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户巡检任务详情信息失败,instId:<{}>,inspectTaskNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", inspectTaskDetail.getInstId(), inspectTaskDetail.getInspectTaskNo(), inspectTaskDetail.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public List<InspectTaskDetail> selectBspsStatisInspectDetailByManagerId(String instId, String managerId, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsStatisInspectDetailByManagerId");
        InspectTaskDetail inspectTaskDetail = new InspectTaskDetail();
        inspectTaskDetail.setInstId(instId);
        inspectTaskDetail.setManagerId(managerId);
        inspectTaskDetail.setInspectBeginTime(startDate);
        inspectTaskDetail.setInspectEndTime(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取待巡检任务统计信息失败,instId:<{}>,managerId:<{}>,inspectBeginTime:<{}>,inspectEndTime:<{}>", instId, managerId, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBspsInspectDetailByManagerId(String instId, InspectTaskDetail inspectTaskDetail, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsInspectDetailByManagerId");
        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, inspectTaskDetail, InspectTaskDetail.class, pageInfo);
        if (null == pageResult || CollectionUtils.isEmpty(pageResult.getResult())) {
            rglog.error("获取待巡检任务信息失败,instId:<{}>,managerId:<{}>", instId);
            return null;
        }
        return pageResult;
    }

}
